home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d3
/
rettig.arc
/
TRSOURCE.EXE
/
FILEREAD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-22
|
3KB
|
84 lines
/*********
* Syntax: FILEREAD( [<drive>:][\<path>]<filename.ext> )
* By: Tom Rettig, Leonard Zerman
* Placed in the public domain by Tom Rettig Associates, 10/22/1990.
*
* Return: <expC> contents of a disk text file.
* <expC> error message if any trouble.
*********/
#include "trlib.h"
TRTYPE fileread()
{
static char funcname[] = { "fileread" };
char *filename, *ret, *tempret;
int filenum;
int i, status;
int readbytes;
long filebytes, maxbytes;
long holdbytes;
if ( PCOUNT==1 && ISCHAR(1) )
{
filename = _parc(1);
filenum = _tr_open( filename, READONLY );
if ( filenum != FILEERROR && *filename )
{
filebytes = _tr_lseek( filenum, 0L, 2 ); /* eof */
if ( filebytes != (long)FILEERROR )
{
maxbytes = filebytes;
holdbytes = maxbytes;
ret = _tr_allocmem((unsigned)maxbytes+1); /* +1 for nullc */
if ( ret && maxbytes < 65535)
{
_tr_lseek( filenum, 0L, 0 ); /* position to bof */
i = 0;
tempret = ret;
while (maxbytes > 32767L)
{
if (status = _tr_read( filenum, tempret, 32767) <= 0)
{
_retc( _tr_errmsgs(funcname,E_FREAD) );
_tr_close( filenum );
_tr_freemem( ret,(unsigned)holdbytes+1 );
}
tempret += 32767;
maxbytes -= 32767L;
}
readbytes = _tr_read( filenum, tempret, maxbytes );
if ( readbytes != (long)FILEERROR )
{
ret[(unsigned)readbytes-1] = NULLC; /* replace eof with nullc */
_retc( ret );
_tr_close( filenum );
_tr_freemem( ret,(unsigned)holdbytes+1 );
}
else
{
_retc( _tr_errmsgs(funcname,E_FREAD) );
_tr_close( filenum );
_tr_freemem( ret, (unsigned)holdbytes+1 );
}
}
else
{
_retc( _tr_errmsgs(funcname,E_ALLOC) );
_tr_close( filenum );
}
}
else
{
_retc( _tr_errmsgs(funcname,E_FSEEK) );
_tr_close( filenum );
}
}
else
_retc( _tr_errmsgs(funcname,E_FOPEN) );
}
else
_retc( _tr_errmsgs(funcname,E_SYNTAX) );
}
/* eof fileread */